library(tidyverse)

domain_df <- read_tsv("data/raw/domain_reference_table.tsv")

other_scores <- read_tsv("data/robertson_bakshy_scores.tsv")
other_scores <- other_scores |> rename(
    robertson_score = bias_score,
    bakshy_score = bias_fb_score) |>
    select(domain, robertson_score, bakshy_score)

domain_df <- left_join(domain_df, other_scores, by="domain")

robertson_pearson <- cor(
    domain_df$domain_score_continuous_political, 
    domain_df$robertson_score, 
    use="pairwise.complete.obs", 
    method = "pearson") |> round(3)
robertson_spearman <- cor(
    domain_df$domain_score_continuous_political, 
    domain_df$robertson_score, 
    use="pairwise.complete.obs", 
    method = "spearman") |> round(3)

robertson_plot <- ggplot(domain_df, 
       aes(x=domain_score_continuous_political, 
           y = robertson_score)) + 
    geom_point(alpha = 0.5) + 
    coord_cartesian(xlim=c(-1, 1), ylim=c(-1, 1)) + 
    geom_abline(slope=1, intercept=0, linetype='dashed') + 
    labs(
        title = bquote("URL (r ="~.(robertson_pearson)~","~rho~"="~.(robertson_spearman)~")"),
        x = "Domain Score (Twitter)",
        y = "Robertson et al. Score",
        ) +
        theme_classic()+
        theme(text = element_text(family = "serif"),
              plot.title = element_text(size = 24, face = "bold"),
              plot.subtitle = element_text(size = 20, face = "plain"),
              axis.text = element_text(size=18),
              axis.title = element_text(size=18))

ggsave("results/fig_d1.pdf", robertson_plot, width = 12, height=8, dpi = 450)
ggsave("results/robertson_plot.pdf", robertson_plot, width = 12, height=8, dpi = 450)


bakshy_pearson <- cor(
    domain_df$domain_score_continuous_political, 
    domain_df$bakshy_score, 
    use="pairwise.complete.obs",
    method = "pearson") |> round(3)
bakshy_spearman <- cor(
    domain_df$domain_score_continuous_political, 
    domain_df$bakshy_score, 
    use="pairwise.complete.obs", 
    method = "spearman") |> round(3)
bakshy_plot <- ggplot(domain_df, 
       aes(x=domain_score_continuous_political, 
           y = bakshy_score)) + 
    geom_point(alpha = 0.5) + 
    theme_classic() + 
    theme(text = element_text(family = "serif"),
         plot.title = element_text(size = 24, face = "bold"),
         plot.subtitle = element_text(size = 20, face = "plain"),
         axis.text = element_text(size=18),
         axis.title = element_text(size=18)) +
    coord_cartesian(xlim=c(-1, 1), ylim=c(-1, 1)) + 
    geom_abline(slope=1, intercept=0, linetype='dashed') + 
    labs(
        title = bquote("URL (r ="~.(bakshy_pearson)~","~rho~"="~.(bakshy_spearman)~")"),
        x = "Domain Score (Twitter)",
        y = "Bakshy et al. Score",
        )

ggsave("results/fig_d2.pdf", bakshy_plot, width = 12, height=8, dpi = 450)
ggsave("results/bakshy_plot.pdf", bakshy_plot, width = 12, height=8, dpi = 450)
